package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.NotesPayabledLinesEntity;
import cn.com.huzo.repository.purchase.NotesPayabledLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.purchase.NotesPayabledLinesManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import huzofw.model.entities.paginator.Paginator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 对账单明细管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("NotesPayabledLinesManager")
public class NotesPayabledLinesManagerImpl extends GenericManagerImpl<NotesPayabledLinesEntity, String>
        implements NotesPayabledLinesManager {

    @Resource(name = "NotesPayabledLinesRepository")
    private NotesPayabledLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        // 对账明细表
        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_lines.fetchForCount(criterion);
        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<NotesPayabledLinesEntity> itemList = _dao_lines.fetchForPage(criterion);
            Paginator<NotesPayabledLinesEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(Map<String, Object> criterion) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        List<NotesPayabledLinesEntity> itemList = mapper.convertValue(MapUtils.getObject(criterion, "ItemList"), new TypeReference<List<NotesPayabledLinesEntity>>() {
        });
        Integer rowsAffected = _dao_lines.batchUpdate(itemList);

        if (rowsAffected > 0) {
            // 审计日志
            //writeAuditLog(itemList.get(0).getUpdatedBy(), "采购管理→对账单明细", "订单号：" + itemList.get(0).getBillNo(), "更新");
        }

        Map<String, Object> _result = new HashMap();
        _result.put("TotalAmount", _dao_lines.fetchTotalAmount(criterion));   // 本次对账总金额
        _result.put("LinesData", this.fetchForPage(criterion));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        _dao_lines.delete(criterion.get("LinesId").toString());

        return this.fetchForPage(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(Map<String, Object> criterion) throws Exception {
        // 第一步：批量删除
        ObjectMapper mapper = new ObjectMapper();
        List<NotesPayabledLinesEntity> _deletedDataList = mapper.convertValue(MapUtils.getObject(criterion, "DeletedDataList"), new TypeReference<List<NotesPayabledLinesEntity>>() {
        });
        for (int i = 0; i < _deletedDataList.size(); i++) {
            _dao_lines.delete(_deletedDataList.get(i).getLinesId());
        }

        // 第二步：返回删除后的数据
        return this.fetchForPage(criterion);
    }
}
